<?php
class Model_Order extends Zend_Db_Table_Abstract{
	protected $db;
	public function __construct() {
		$this->db = Zend_Registry::get ( "db" );
	}
	
	public function getItems($options) {
		$table = 'orders';
		$result = array ();
		$query = $this->db->query ( "SELECT COUNT(*) AS Total FROM $table" );
		$row = $query->fetchAll ();
		$result ["total"] = $row [0] ['Total'];	
		foreach($options as $key => $value) {
			$$key = trim($value);
		}
		$cond = '1=1';
		if($kw) {
			$cond .= ' and `description` like \'%'.mysql_escape_string($kw).'%\'';
		}
		
		if($d) {
			$ds = explode('/', $d);
			if(strlen($ds[0]) == 1) $ds[0] = '0' . $ds[0];
			if(strlen($ds[1]) == 1) $ds[1] = '0' . $ds[1];
			$d = $ds[2]. '-' . $ds[0] . '-' . $ds[1];
			$cond .= " and `dates` like '%".$d."%'";
		}
		
		$query = $this->db->query ( "SELECT * FROM $table WHERE $cond order by  $sort $order LIMIT $offset, $rows" );
		$result ['rows'] = $query->fetchAll ();
		
		return $result;
	}
	
	public function create($data) {
		$items = $data['items'];
		unset($data['items']);
		if($data['customerPassport']) {
			$passport = $data['customerPassport'];
			$table = new Model_Table();
			if ($customer = $table->selectCond('customer', "passport='$passport'")) {
				foreach($customer as $key => $val) {
					if(in_array($key, array('name', 'passport', 'phone', 'address', 'company', 'companyAddress', 'companyPhone', 'companyFax'))) {
						$data['customer' . strtoupper($key[0]).substr($key, 1)] = $val;
					}
				}
			} else {
				$table->addObj('customer', array('name' => $data['customerName'], 'passport'=> $data['customerPassport']));
			}
		}
		$this->db->insert('orders', $data);
		$id = $this->db->lastInsertId();
		$names = array();
		$dates = array();
		foreach($items as $item) {
			$item['orderId'] = $id;
			$this->db->insert('orderProduct', $item);
			$names[] = $item['productName'];
			if(@$item['startTime'] && strtotime($item['startTime']))
				$dates[] = $item['startTime'];
			if(@$item['endTime'] && strtotime($item['endTime']))
				$dates[] = $item['endTime'];
		}
		$description = implode(',', $names);
		$dates = array_unique($dates);
		$this->db->update('orders', array('description' => $description, 'dates' => implode(',', $dates)), 'id='. $id);
	}
	
	public function update($data) {
		$items = $data['items'];
		$id = $data['id'];
		unset($data['items']);
		unset($data['id']);
		$this->db->update('orders', $data, 'id=' . $id);
		$table = new Model_Table();
		$order = $table->select('orders', $id);
		$close = false;
		if ($order) {
			if (!$order['payment'] && @$data['payment']) {
				// chuyen trang thai thanh toan
				$close = true;
			}
		}
		$this->clearItems($id);
		$names = array();
		$dates = array();
		foreach($items as $item) {
			$item['orderId'] = $id;
			$this->db->insert('orderProduct', $item);
			$names[] = $item['productName'];
			if(@$item['startTime'] && strtotime($item['startTime']))
				$dates[] = $item['startTime'];
			if(@$item['endTime'] && strtotime($item['endTime']))
				$dates[] = $item['endTime'];
			if ($close) {
				if ($item['productType'] == 'Service') {
					$product = $table->select('catalog', $item['productId']);
					if($product) {
						$qty = $product['quantity'] - $item['quantity'];
						$this->db->update('catalog', array('quantity' => $qty), 'id=' . $item['productId']);
					}
				}
			}
		}
		$description = implode(',', $names);
		$dates = array_unique($dates);
		$this->db->update('orders', array('description' => $description, 'dates' => implode(',', $dates)), 'id='. $id);
	}
	
	public function load($id) {
		$order = $this->db->query('select * from orders where id=' . $id)->fetchAll();
		if($order) $order = $order[0];
		$order['items'] = $this->db->query('select * from orderProduct where orderId=' . $id . ' order by id asc')->fetchAll();
		return $order;
	}
	
	public function clearItems($id){
		return $this->db->delete('orderproduct', 'orderId=' . $id);
	}
	
	public function delete($id) {
		$this->db->delete('orders', 'id=' . $id);
		$this->clearItems($id);
		
	}
}